package com.elsa.smart_buffet.controller;

import com.alibaba.fastjson.JSON;
import com.elsa.smart_buffet.pojo.Evaluate;
import com.elsa.smart_buffet.pojo.ResultBox.ResponseResult;
import com.elsa.smart_buffet.service.EvaluateService;
import com.elsa.smart_buffet.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
public class EvaluateController {

    @Autowired
    private EvaluateService evaluateService;

    @GetMapping("/evaluate/list")
    public ResponseResult selectAllEvaluate() {
        return evaluateService.selectAllEvaluate();
    }

    @GetMapping("/evaluate/{id}")
    public ResponseResult selectOneByIdEvaluate(@PathVariable("id") int id) {
        return evaluateService.selectOneByIdEvaluate(id);
    }

    @PutMapping("/evaluate")
    public ResponseResult updateEvaluate(@RequestBody Evaluate evaluate) {
        return evaluateService.updateEvaluate(evaluate);
    }

    @DeleteMapping("/evaluate/{id}")
    public ResponseResult deleteByIdEvaluate(@PathVariable int id) {
        return  evaluateService.deleteByIdEvaluate(id);
    }

    @PostMapping("/evaluate")
    public ResponseResult insertEvaluate(@RequestBody String data) {
        Evaluate evaluate = new Evaluate();
        Map<String, Object> data1 = (Map<String, Object>) JSON.parseObject(data).get("data");
        log.info(String.valueOf(data1));
        String opinion = data1.get("opinion").toString();
        evaluate.setEOpinion(opinion);
        String OId = data1.get("OId").toString();
        evaluate.setOId(OId);
        int cId = Integer.parseInt(data1.get("cId").toString());
        evaluate.setCId(cId);
        int score = Integer.parseInt(data1.get("score").toString());
        evaluate.setEScore(score);

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.sql.Date ctime2 = new java.sql.Date(new java.util.Date().getTime());
        evaluate.setETime(df.format(ctime2));
        evaluate.setMId(cId);
        return evaluateService.insertEvaluate(evaluate);
    }

    @PostMapping("/selectEvaluateByDate")
    public ResponseResult selectEvaluateByTime(@RequestBody String time) {
        return evaluateService.selectEvaluateByTime(JSON.parseObject(time).get("time").toString());
    }

    @PostMapping("/selectEvaluateBySearchData")
    public ResponseResult selectEvaluateBySearchData(@RequestBody String data) {
        return evaluateService.selectEvaluateBySearchData(JSON.parseObject(data).get("data").toString());
    }

}
